{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "useful-corps",
   "metadata": {},
   "source": [
    "# Pipeline Tests\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20bc4b82",
   "metadata": {},
   "source": [
    "## Local Scheduler Pipeline Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "59b3786f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scheduler ip 0.0.0.0\n",
      "Mesh URL 0.0.0.0:9000\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "k8s = False\n",
    "if k8s:\n",
    "    SCHEDULER_IP=!kubectl get svc seldon-scheduler -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "    SCHEDULER_IP=SCHEDULER_IP[0]\n",
    "    MESH_IP=!kubectl get svc seldon-mesh -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "    MESH_URL=MESH_IP[0]\n",
    "else:\n",
    "    SCHEDULER_IP=\"0.0.0.0\"\n",
    "    MESH_URL=\"0.0.0.0:9000\"\n",
    "os.environ['SCHEDULER_IP'] = SCHEDULER_IP\n",
    "os.environ['MESH_URL'] = MESH_URL\n",
    "print(\"Scheduler ip\",SCHEDULER_IP)\n",
    "print(\"Mesh URL\",MESH_URL)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "short-distributor",
   "metadata": {},
   "source": [
    "To test pipeline status updates run in a separate terminal\n",
    "\n",
    "```bash\n",
    "grpcurl -d '{\"subscriberName\":\"foo\"}' -plaintext -import-path ../../apis -proto ../../apis/mlops/scheduler/scheduler.proto  0.0.0.0:9004 seldon.mlops.scheduler.Scheduler/SubscribePipelineStatus\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a039351e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{ \\\n",
    "              \"meta\":{\"name\":\"iris\"},\\\n",
    "              \"modelSpec\":{\"uri\":\"gs://seldon-models/mlserver/iris\",\\\n",
    "                           \"requirements\":[\"sklearn\"],\\\n",
    "                           \"memoryBytes\":500},\\\n",
    "              \"deploymentSpec\":{\"replicas\":1}}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/LoadModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b7535bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"name\":\"mlserver\"}' \\\n",
    "         -plaintext \\\n",
    "         -proto ../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/ServerStatus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cb174d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "!curl -v http://${MESH_URL}/v2/models/iris/infer -H \"Content-Type: application/json\" -H \"seldon-model: iris\"\\\n",
    "        -d '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mexican-breakdown",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"pipeline\":{ \\\n",
    "              \"name\": \"pipeline-iris\",\\\n",
    "              \"steps\": [\\\n",
    "                 {\"name\":\"iris\"} \\\n",
    "              ], \\\n",
    "              \"output\":{\"inputs\":\"iris\"}}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/LoadPipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dental-above",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"iris\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"iris\",\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"iris\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"inputs\": [\r\n",
      "            \"iris.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {\r\n",
      "          \r\n",
      "        }\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 1,\r\n",
      "        \"status\": \"PipelineTerminating\",\r\n",
      "        \"lastChangeTimestamp\": \"2022-03-30T15:44:57.317913472Z\"\r\n",
      "      }\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"iris\",\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"iris\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"inputs\": [\r\n",
      "            \"iris.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {\r\n",
      "          \r\n",
      "        }\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 2,\r\n",
      "        \"status\": \"PipelineCreating\",\r\n",
      "        \"lastChangeTimestamp\": \"2022-03-30T15:45:08.545973059Z\"\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!grpcurl -d '{\"name\":\"iris\",\"allVersions\":true}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/PipelineStatus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bb6e929b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*   Trying 0.0.0.0...\r\n",
      "* Connected to 0.0.0.0 (127.0.0.1) port 9000 (#0)\r\n",
      "> POST /v2/models/iris/infer HTTP/1.1\r",
      "\r\n",
      "> Host: 0.0.0.0:9000\r",
      "\r\n",
      "> User-Agent: curl/7.47.0\r",
      "\r\n",
      "> Accept: */*\r",
      "\r\n",
      "> Content-Type: application/json\r",
      "\r\n",
      "> seldon-model: iris.pipeline\r",
      "\r\n",
      "> Content-Length: 94\r",
      "\r\n",
      "> \r",
      "\r\n",
      "* upload completely sent off: 94 out of 94 bytes\r\n",
      "< HTTP/1.1 404 Not Found\r",
      "\r\n",
      "< date: Sat, 02 Apr 2022 08:49:57 GMT\r",
      "\r\n",
      "< server: envoy\r",
      "\r\n",
      "< connection: close\r",
      "\r\n",
      "< content-length: 0\r",
      "\r\n",
      "< \r",
      "\r\n",
      "* Closing connection 0\r\n"
     ]
    }
   ],
   "source": [
    "!curl -v http://${MESH_URL}/v2/models/iris/infer -H \"Content-Type: application/json\" -H \"seldon-model: iris.pipeline\"\\\n",
    "        -d '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "solved-darkness",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!grpcurl -d '{\"name\":\"iris\"}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/UnloadPipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "southwest-husband",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"iris\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"iris\",\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"iris\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"inputs\": [\r\n",
      "            \"iris.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {\r\n",
      "          \r\n",
      "        }\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 1,\r\n",
      "        \"status\": \"PipelineReady\",\r\n",
      "        \"reason\": \"removal requested\",\r\n",
      "        \"lastChangeTimestamp\": \"2022-04-02T08:44:53.263695389Z\"\r\n",
      "      }\r\n",
      "    },\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"iris\",\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"iris\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"inputs\": [\r\n",
      "            \"iris.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {\r\n",
      "          \r\n",
      "        }\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 2,\r\n",
      "        \"status\": \"PipelineCreating\",\r\n",
      "        \"lastChangeTimestamp\": \"2022-04-02T08:45:24.824140452Z\"\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!grpcurl -d '{\"name\":\"iris\",\"allVersions\":true}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/PipelineStatus"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5228b2eb",
   "metadata": {},
   "source": [
    "## K8s Pipeline Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hydraulic-guatemala",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../samples/pipelines/example.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37639bc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl create -f ../../samples/pipelines/example.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7354d511",
   "metadata": {},
   "outputs": [],
   "source": [
    "!curl -v http://${MESH_URL}/v2/models/iris/infer -H \"Content-Type: application/json\" -H \"seldon-model: example.pipeline\"\\\n",
    "        -d '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78b63030",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
